from flask import Flask, render_template, request,session,redirect
from . import kaoshi
import json
from frameworks.utils.JsonResult2 import *
from frameworks.cores.LoadClass import *
import datetime

jsonResult = JsonResult2()
loader = LoadClass()

@kaoshi.route("/shijuan_main_formadd",methods=['GET', 'POST'])
def shijuan_main_formadd():
    model = request.args.get("model", "school")
    wherestr = request.args.get("w", "")
    fields = loader.load("controller.CommonController","CommonController",{
        "func":"getAddMap",
        "fields":["*"],
        "model": model
    })
    if wherestr.find(")") > 0 and wherestr.find("(") > 0:
        whereoption = wherestr.replace(")","").split("(")
        data = {whereoption[0]:whereoption[1]}
    else:
        data = []
    return render_template(
        'kaoshi/shijuan_main_formadd.html',
        fields=fields,
        model=model,
        data=data
    )

@kaoshi.route("/shijuan_main_form",methods=['GET', 'POST'])
def shijuan_main_form():
    model = request.args.get("model", "school")
    id = request.args.get("id", "0")
    fields = loader.load("controller.CommonController", "CommonController", {
        "func": "getEditMap",
        "fields": ["*"],
        "model": model
    })
    obj = loader.load("controller.CommonController","CommonController",{
        "func":"getRowData",
        "fields":["*"],
        "id": id,
        "model": model
    })
    print(obj)
    print(fields)
    return render_template(
        'kaoshi/shijuan_main_form.html',
        obj=obj[0],
        fields=fields,
        model=model
    )

@kaoshi.route("/main_add",methods=['POST'])
def main_add():
    model = request.args.get("model", "school")
    data = {}
    for key in request.form.keys():
        data[key] = request.form.get(key)
    if "userid" in data:
        data["userid"] = session["userid"]
    rs = loader.load("controller.ExamPaperController", "ExamPaperController", {
        "func": "getBatchQuestion",
        "data": data,
        "model": model
    })
    return jsonResult.setOk([], "添加成功")